﻿package com.springmvc.web.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.springmvc.web.entity.LoginForm;
import com.springmvc.web.entity.Pager;
import com.springmvc.web.entity.User;
import com.springmvc.web.service.ILoginService;
import com.springmvc.web.service.IUserService;

@Controller
@RequestMapping("/data")
public class UserController {

		@Autowired
		private  IUserService userService;
		 
		@Autowired
	    private ILoginService loginService ;
		
			 		 		 	
		@RequestMapping("/getAllUser")
		public String getAllUser(){
			   return "AllUser";				     	    			   
		}
		
		@RequestMapping("/toPersonal")
		public String toPersonal(){
			   return "personalPage";
		}


     	//添加用户
		@ResponseBody 
		@RequestMapping(value="/addUser",produces="application/json")
	    public String addUser(User user,ModelMap modelMap,LoginForm login,HttpServletRequest request,HttpServletResponse response){
		    
			try{
				 if(StringUtils.isBlank(user.getUsername())){
						modelMap.put("error1", "用户名不能为空!");
					    return "addUser";
			         }
				 if(StringUtils.isBlank(user.getPassword())){
				      
			  			modelMap.put("error1", "密码不能为空!");
						     
						return "addUser";
					}
				 
				 
		//验证用户名是否存在
		LoginForm loginform = this.loginService.getByUsername(login.getUsername());
		
			if(loginform != null){
				
				 modelMap.put("error1", "用户已存在!");
				     return "addUser";
			 }							
			   }catch(Exception e){				   
				     e.printStackTrace();
			   }				
			   /*//时间戳自动获取ID			
			   long id = System.currentTimeMillis();
			   user.setId(String.valueOf(id));*/			  			   
			   this.userService.addUser(user);			   
			   this.loginService.createLogin(login);		   		    
			   
			   modelMap.put("user", this.userService.getAllUser());			 
			         
			         return "1"; 			 
			  }
	
	
		//删除用户
		@ResponseBody
		@RequestMapping(value="/delUser",produces="application/json")
		public String delUser(Integer id,ModelMap modelMap,HttpServletRequest request){
			
			  this.userService.delUser(id);
										
			  modelMap.put("user", this.userService.getAllUser());
			    			
				     return "1";
			}
				
	   
		//获取用户
		@RequestMapping("/toGetUser")
		public String getUser(Integer id,ModelMap modelMap,HttpServletRequest request){
			
			  modelMap.put("user", this.userService.getUser(id));
						    
					 return "editUser";		
		    }
				
      
		//编辑用户
		@ResponseBody
		@RequestMapping(value="/updateUser",produces="application/json")
		public String updateUser(User user,ModelMap modelMap,HttpServletRequest request){
						
			  this.userService.updateUser(user);
								
			  modelMap.put("user", this.userService.getAllUser());
			
				    return "1";	      		
		    }
		
		
		//分页获取数据
		@SuppressWarnings({ "rawtypes", "unchecked" })
		@RequestMapping(value="/getUserByPage",produces="application/json")
		public @ResponseBody Map getUserByPage(Pager pager,User user,HttpServletRequest request,HttpServletResponse response){
			
				Map map0 = new HashMap();
				int total = this.userService.getTotal();
				pager.setTotal(total);
				
				int rows = pager.getRows();
				int startrow =  pager.getStartrow();
				
				Integer id = user.getId();
				String username = user.getUsername();
				
				List<User> users = this.userService.selectUserByPager(rows, startrow, id, username);
									
					map0.put("rows", users);
					map0.put("total", total);
		
					return map0;
		    }
				
				
	   
	   @SuppressWarnings({ "rawtypes", "unchecked" })
	   @RequestMapping(value="/getUserByName",produces="application/json")		
	   public @ResponseBody Map getUserByName(Pager pager,HttpServletRequest request,HttpSession session){		  		    
		    
		         Map person = new HashMap();
		         int total = this.userService.getTotal();
				 pager.setTotal(total);
		         
				 int rows = pager.getRows();
				 int startrow =  pager.getStartrow();
				
				 String username =(String)session.getAttribute("username");
		           
				 List<User> user0 = this.userService.getUserByName(rows,startrow,username);
		   		          
		         person.put("rows", user0);
		         person.put("total", total);
		         
		         return person;
	       } 
		
		
	    	   
}		


		/*	@ResponseBody
		@RequestMapping("/getPsById")
		public String getPsById(Integer id,ModelMap modelMap,HttpServletRequest request){
			
				modelMap.put("user", this.userService.getUser(id));
				
				return "personalEditUser";
			}*/



